﻿using System;
using System.Collections.Generic;
using System.Text;
using WorkflowCore.Interface;

namespace Zt.QmtqDemo.ConfmPlan.Saga
{
    public class ConfmPlanWorkflow : IWorkflow<ConfPlanData>
    {
        public string Id => "confmplan";
        public int Version => 1;

        public void Build(IWorkflowBuilder<ConfPlanData> builder)
        {
            builder
                .StartWith(context => Console.WriteLine("Begin"))
                .Saga(saga => saga
                    .StartWith<MatTraceTask>()
                    .Input(step => step.MatDtos ,data => data.MatDtos)
                    .Input(step => step.PlanNo, data => data.PlanNo)
                    .Output(data => data.Message, step => step.Message)
                    .Output(data =>data.Code, step => step.Code) 
                        .CompensateWith<UndoEverything>(compensate => {
                            compensate
                            .Output(data => data.Message, step => step.Message)
                            .Output(data => data.Code, step => step.Code);
                        })
                    .Then<OrderTraceTask>()
                    .Input(step => step.MatDtos, data => data.MatDtos)
                    .Output(data => data.Message, step => step.Message)
                    .Output(data => data.Code, step => step.Code)
                        .CompensateWith<UndoEverything>(compensate => {
                            compensate
                            .Output(data => data.Message, step => step.Message)
                            .Output(data => data.Code, step => step.Code);
                        })
                    .Then<ConfmPlanTask>()
                       .Input(step=>step.PlanId, data=>data.PlanId)
                        .Input(step => step.MatDtos, data => data.MatDtos)
                        .Input(step =>step.PlanCode, data=>data.PlanNo)
                        .Input(step => step.OrderNo, data => data.OrderNo)
                        .Output(data => data.Message, step => step.Message)
                        .Output(data => data.Code, step => step.Code)
                        .CompensateWith<UndoEverything>(compensate => {
                            compensate
                            .Output(data => data.Message, step => step.Message)
                            .Output(data => data.Code, step => step.Code);
                        })
                    )
                    //.CompensateWith<UndoEverything>(compensate => {
                    //    compensate
                    //    .Output(data => data.Message, step => step.Message)
                    //    .Output(data => data.Code, step => step.Code);
                    //})
                .Then(context => Console.WriteLine("End"));
        }
    }
}
